import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_first_one/pages/home/home.dart';
import 'package:get/get.dart';
import 'package:flutter_first_one/components/story_card_list.dart';
import 'package:get/get_rx/get_rx.dart';
import 'package:flutter_first_one/custom_behavior/keep_alive_warpper.dart';
import 'package:flutter_first_one/util/page_load.dart';
import 'package:flutter_first_one/api/story.dart';
import 'package:flutter_first_one/custom_behavior/scroll_behavior.dart';

class Story extends StatefulWidget {
  const Story({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _StoryState createState() => _StoryState();
}

class _StoryState extends State<Story> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 3);
    _tabController.addListener(_handleTabChange);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _handleTabChange() {
    print("Tab changed: ${_tabController.index}");
  }

  final List<String> imageUrls = [
    'https://img1.baidu.com/it/u=3007039639,472852969&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1711126800&t=3de2540bcbc57b8510af29c344724f1c',
    'https://img1.baidu.com/it/u=3007039639,472852969&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1711126800&t=3de2540bcbc57b8510af29c344724f1c',
    'https://img1.baidu.com/it/u=3007039639,472852969&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1711126800&t=3de2540bcbc57b8510af29c344724f1c',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('儿时故事'),
        centerTitle: true,
        titleTextStyle: const TextStyle(fontSize: 18, color: Colors.black),
      ),
      // tab的监视器组件，如果该页面有tab组件则需要DefaultTabController放置最外层，
      // 里面就可以在任意位置放置TabBar、TabBarView组件
      body: DefaultTabController(
        length: 3,
        child: Column(
          children: [
            Text('搜索框'),
            // tab的列表，放置哪里都行
            TabBar(
              // 添加控制器，flutter的监视变化逻辑都是这样：
              // 1.添加控制器对象
              // 2.控制器对象在initState时添加监视对象（每一个widget都不一样）
              // 3.监视对象里面传触发的方法
              // 4.在widget变化时方法就会被触发
              controller: _tabController,
              tabs: const [
                Tab(text: 'Tab 1'),
                Tab(text: 'Tab 2'),
                Tab(text: 'Tab 3'),
              ],
            ),
            Expanded(
              child: CusScrollBehavior(
                child: TabBarView(
                  // 添加监视对象，需与配套使用的widget同个对象
                  controller: _tabController,
                  children: const [
                    // 缓存tab组件
                    KeepAliveWrapper(
                        child: StoryCardList(
                      requestApi: StoryApi.collectRecord,
                      params: {'type': "collect"},
                    )),
                    KeepAliveWrapper(
                        child: StoryCardList(
                      requestApi: StoryApi.collectRecord,
                      params: {'type': "collect"},
                    )),
                    KeepAliveWrapper(
                        child: StoryCardList(
                      requestApi: StoryApi.collectRecord,
                      params: {'type': "collect"},
                    )),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
